import { Controller, Get, Post, Body, Patch, Param, Delete, HttpException, Res } from '@nestjs/common';
import { TagService } from './tag.service';
import { CreateTagDto } from './dto/create-tag.dto';
import { UpdateTagDto } from './dto/update-tag.dto';
import { DeleteTagDto } from './dto/delete-tag.dto';
import { TagDocument } from './shema/tag.shema';

@Controller('tag')
export class TagController {
  constructor(private readonly tagService: TagService) {}

  @Post('create')
  async create(@Body() createTagDto: CreateTagDto):Promise<TagDocument>{
    const isRepeat=await this.tagService.findOneByTagName(createTagDto.tag)
    if(isRepeat){
      throw new HttpException('该标签已经被创建了',400)
    }
    return await this.tagService.create(createTagDto);
  }

  @Post('list')
  findAll() {
    return this.tagService.findAll();
  }
  
  @Post('update')
  async update(@Body() updateTagDto: UpdateTagDto):Promise<Boolean>{
    return await this.tagService.update(updateTagDto);
  }

  @Post('delete')
  async delete(@Body() deleteTagDto:DeleteTagDto):Promise<Boolean>{
    return await this.tagService.deleteOne(deleteTagDto.id);
  }
}
